'use strict';

/* Controllers */
app.controller('houseOwnerListCtl', function($scope, $http, rest, $compile, $location, ngDialog, user, $state, $window, uiLoad, $rootScope, $ocLazyLoad) {
    $scope.queryParams = {};
    $scope.checkStatus = {};
    $scope.decorateStatus = {};
    $scope.powerTypeArray = [{
        "name": "全部",
        "value": -1
    }, {
        "name": "通电",
        "value": 0
    }, {
        "name": "断电",
        "value": 1
    }];

    $scope.lockTypeArray = [{
        "name": "全部",
        "value": -1
    }, {
        "name": "未安装",
        "value": 0
    }, {
        "name": "锁电安装中",
        "value": 3
    }, {
        "name": "锁电安装成功",
        "value": 4
    }];
    var tmpHouseIdArr = "";

    // function time(mydate) {
    //     var str = mydate.substring(0, 10);
    //     var dataArr = str.split('-');
    //     var timeStr = dataArr[0] + "年" + dataArr[1] + "月" + dataArr[2] + "日";
    //     return timeStr;
    // }

    initDataTables();

    function initDataTables() {
        $scope.dataTableSettings = {
            "dom": "tipr",
            "sAjaxSource": "4008",
            "bAutoWidth": true,
            "aoColumns": [
                // {
                //     "mDataProp": "cityName",
                //     "sTitle": "电表ID",
                //     "sDefaultContent": "",
                //     "sClass": "center"
                // }, 
                {
                    "mDataProp": "devProducer",
                    "sTitle": "设备厂商",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "areaName",
                    "sTitle": "小区",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "buildingNo",
                    "sTitle": "楼栋",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "roomCode",
                    "sTitle": "房间号",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "tenantsCount",
                    "sTitle": "绑定用户",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "leftElectricity",
                    "sTitle": "剩余电量(度)",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "ammeterStatus",
                    "sTitle": "电表状态",
                    "sDefaultContent": "",
                    "sClass": "center"
                }, {
                    "mDataProp": "operate",
                    "sTitle": "操作",
                    "sDefaultContent": "",
                    "sClass": "center handle-td operation"
                }
            ],
            "fnRowCallback": function(nRow, aData, iDisplayIndex) {
                var btnStr = "";
                var houseBtnStr = "";
                var contractBtnStr = "";
                var decorateBtnStr = "";

                btnStr += "<div class='btn btn-new' ui-toggle-class='active' target='.asideDetails' ng-click='viewElectricDetails(\"" + aData.houseId + "\")'>查看详情</div>"

                if (aData.tdStatus == 1) {
                    decorateBtnStr += "<div class='btn btn-new' ng-click='powerOff(\"" + aData.houseId + "\")'>断电</div>"
                } else {
                    decorateBtnStr += "<div class='btn btn-new' ng-click='powerOn(\"" + aData.houseId + "\")'>通电</div>"
                }

                $(nRow).find(".handle-td").append($compile(decorateBtnStr)($scope));
                $(nRow).find(".handle-td").append($compile(btnStr)($scope));
                // $(nRow).find(".handle-td").prev().empty().append(time(aData.createDate));

                tmpHouseIdArr += aData.houseId + ",";
                if (iDisplayIndex >= 9) {
                    // console.log(tmpHouseIdArr);
                }
            },
            "fnDrawCallback": function(oSettings) {
                $scope.checkStatusValue = $scope.queryParams.checkStatus;
                if ("number" == typeof $scope.checkStatusValue) {
                    $scope.checkTypeArray.map(function(ele, index) {
                        if ($scope.checkStatusValue == ele.value) {
                            $scope.checkStatus.selected = ele;
                        }
                    })
                }
                $scope.decorateStatusValue = $scope.queryParams.decorateStatus;
                if ("number" == typeof $scope.decorateStatusValue) {
                    $scope.lockTypeArray.map(function(ele, index) {
                        if ($scope.decorateStatusValue == ele.value) {
                            $scope.decorateStatus.selected = ele;
                        }
                    })
                }
            },
            "fnServerParams": function(aoData) {
                if ($scope.checkStatus && $scope.checkStatus.selected) {
                    $scope.queryParams.checkStatus = $scope.checkStatus.selected.value
                }
                if ($scope.decorateStatus && $scope.decorateStatus.selected) {
                    $scope.queryParams.decorateStatus = $scope.decorateStatus.selected.value
                }
                aoData.push({
                    'name': 'queryParams',
                    'value': $scope.queryParams
                });
                return aoData;
            }
        }
    }

    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }

    //监听房间号
    $scope.$watch('_roomCode', function(newValue, oldValue) {
            $scope.queryParams.roomCode = newValue;
        })
        //监听小区名称
    $scope.$watch('_areaName', function(newValue, oldValue) {
            $scope.queryParams.areaName = newValue;
        })
        //监听详细地址
    $scope.$watch('_address', function(newValue, oldValue) {
        $scope.queryParams.address = newValue;
    })

    $scope.priceMange = function() {
        ngDialog.openConfirm({
            template: 'js/module/fund/priceMange.html',
            resolve: {
                load: function() {
                    return uiLoad.load(['js/module/fund/priceMange.js'])
                }
            },
            data: {

            },
            scope: $scope,
            width: 600,
            // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    }



    // $scope.queryUser = function(){
    //     if($scope.queryParams.checkStatus && $scope.queryParams.checkStatus.selected){
    //         var checkStatusCache = $scope.queryParams.checkStatus.selected;
    //         if($scope.queryParams.checkStatus.selected){
    //             $scope.queryParams.checkStatus = $scope.queryParams.checkStatus.selected.value
    //         }
    //         $scope.dataTable.fnClearTable();
    //         $scope.queryParams.checkStatus = {};
    //         $scope.queryParams.checkStatus.selected = {};
    //         $scope.queryParams.checkStatus.selected = checkStatusCache;
    //     }else if($scope.queryParams.decorateStatus && $scope.queryParams.decorateStatus.selected){
    //         var decorateStatusCache = $scope.queryParams.decorateStatus.selected;
    //         if($scope.queryParams.decorateStatus.selected){
    //             $scope.queryParams.decorateStatus = $scope.queryParams.decorateStatus.selected.value
    //         }
    //         $scope.dataTable.fnClearTable();
    //         $scope.queryParams.decorateStatus = {};
    //         $scope.queryParams.decorateStatus.selected = {};
    //         $scope.queryParams.decorateStatus.selected = decorateStatusCache;
    //     }else{
    //         $scope.dataTable.fnClearTable();
    //     }

    // };
    $rootScope.details = {};
    $scope.viewElectricDetails = function(houseId) {
        $rootScope.details = houseId;
        // console.log($rootScope.details);
        // $(".backModel").prop('display', 'block');
        document.getElementById('backModel').style.display = 'block';
    }

    $scope.viewOrderReturnState = function(e, houseId) {
        $window.localStorage.setItem('houseId', JSON.stringify({
            id: houseId
        }));
        $state.go('app.houseOwner.viewHouse', {
            id: houseId
        });
    }
    $scope.updateOrderReturnState = function(e, houseId) {
        $window.localStorage.setItem('houseId', JSON.stringify({
            id: houseId
        }));
        $state.go('app.houseOwner.editHouse', {
            id: houseId
        });
    }

    //通电弹窗
    $scope.powerOn = function(e, houseId) {
            ngDialog.openConfirm({
                template: 'js/module/fund/powerOn.html',
                resolve: {
                    load: function() {
                        return uiLoad.load(['js/module/fund//powerOn.js'])
                    }
                },
                data: {

                },
                scope: $scope,
                width: 410,
                // height: 700
            }).then(function(data) {
                initDataTables();
            }, function(reason) {
                initDataTables();
            });
        }
        //断电弹窗
    $scope.powerOff = function(e, houseId) {
        ngDialog.openConfirm({
            template: 'js/module/fund/powerOff.html',
            resolve: {
                load: function() {
                    return uiLoad.load(['js/module/fund/powerOff.js'])
                }
            },
            data: {

            },
            scope: $scope,
            width: 410,
            // height: 700
        }).then(function(data) {
            initDataTables();
        }, function(reason) {
            initDataTables();
        });
    }

    $("#backModel").on('click', function(event) {
        document.getElementById('backModel').style.display = 'none';
        $('.asideDetails').removeClass("active");
    });

});


app.controller('viewElectricDetailsCtrl', function($scope, user, rest, tipService, $location, $rootScope) {

    $rootScope.$watch('details', function(newValue, oldValue) {
        console.log(newValue)
    });

})